/**
 * Created by cz on 2017/5/4.
 */
import React,{Component}from "react";
import {
    View,
    Text,
    StyleSheet,
    TouchableOpacity,
}from "react-native";
import HttpUtil from "./js/common/HttpUtil";
export default class Fetch extends Component{
    constructor(props){
       super(props);
        this.state={
            result:"",
            postData:""
        }
    }
/*    onLoad(url){
        fetch(url)
            .then(response=>response.json())
            .then(result=>{
                    this.setState({
                        result:JSON.stringify(result)
                    })
            })
            .catch(error=>{
                this.setState({
                    result:JSON.stringify(error)
                })
            })
    }
    submitLoad(url,data){
        fetch(url,{
            method:"POST",
            header:{
                "Accept":"application/json",
                "Content-Type":"application/json"
            },
            body: JSON.stringify(data)
        }).then(response=>response.json())
        .then(result=>{
            this.setState({
                postData:JSON.stringify(result)
            })
        }).catch(error=>{
            this.setState({
                postData:JSON.stringify(error)
            })
        })
    }*/
    onLoad(url){
        HttpUtil.get(url)
        .then(result=>{
            this.setState({
                result:JSON.stringify(result)
            })
        })
        .catch(error=>{
            this.setState({
                result:JSON.stringify(error)
            })
        })
    }
    submitLoad(url,data){
        HttpUtil.post(url,data)
            .then(result=>{
                this.setState({
                    postData:JSON.stringify(result)
                })
            })
            .catch(error=>{
                this.setState({
                    postData:JSON.stringify(error)
                })
            })
    }
    render(){
        return(
            <View style={styles.container}>
                <TouchableOpacity onPress={()=>this.onLoad('http://rapapi.org/mockjsdata/18390/querList')}>
                    <Text>获取数据</Text>
                </TouchableOpacity>
                <Text>返回结果：{this.state.result}</Text>
                <TouchableOpacity onPress={()=>this.submitLoad('http://rapapi.org/mockjsdata/18390/postUserInfo',{userName:"admin",password:"123"})}>
                    <Text>提交数据</Text>
                </TouchableOpacity>
                <Text>提交返回结果：{this.state.postData}</Text>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container:{
        flex:1
    }
});